<template>
  <div class="subjects-container">
    <el-card class="box-card">
      <!-- 头部区域 -->
      <div class="box-one">
        <div class="one-left">
          <span class="one-left-span">学科名称</span>
          <span>
            <el-input v-model.trim="input" class="one-left-input"></el-input>
          </span>
          <span>
            <el-button size="small" @click="clearInput">清除</el-button>
            <el-button type="primary" size="small" @click="lookupList"
              >搜索</el-button
            >
          </span>
        </div>
        <div class="right">
          <el-button
            class="el-icon-edit"
            type="success"
            @click="dialogVisible = true"
            >新增学科</el-button
          >
          <!-- 新增学科弹框 -->
          <el-dialog
            title="新增学科"
            :visible.sync="dialogVisible"
            width="30%"
            :before-close="handleClose"
          >
            <span>
              <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                class="demo-ruleForm"
              >
                <el-form-item label="学科名称" prop="name">
                  <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="是否显示" prop="delivery">
                  <el-switch
                    v-model="ruleForm.value"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                  >
                  </el-switch>
                </el-form-item>
              </el-form>
            </span>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="addList">确 定</el-button>
            </span>
          </el-dialog>
        </div>
      </div>
      <div class="box-two">
        <i class="el-icon-info"></i>
        <span>数据一共&nbsp;{{ counts }}&nbsp;条</span>
      </div>
      <!-- 主体部分 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column prop="subjectName" label="学科名称" width="145">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="140">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="160">
          <template slot-scope="{ row }">{{
            row.addDate | parseTimeByString
          }}</template>
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示">
          <template slot-scope="{ row }">{{
            row.isFrontDisplay === 1 ? "是" : "否"
          }}</template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column label="操作" width="240">
          <template slot-scope="{ row }">
            <el-link type="primary" @click="goCatalog(row)">学科分类</el-link>
            <el-link type="primary" @click="golabel(row)">学科标签</el-link>
            <el-link type="primary" @click="editList(row)">修改</el-link>
            <!-- 修改弹框 -->
            <el-dialog
              title="新增学科"
              :visible.sync="isdialogVisible"
              width="30%"
              :before-close="handleClose"
            >
              <span>
                <el-form
                  :model="editruleForm"
                  :rules="rules"
                  ref="ruleForm"
                  label-width="100px"
                  class="demo-ruleForm"
                >
                  <el-form-item label="学科名称" prop="name">
                    <el-input v-model="editruleForm.name"></el-input>
                  </el-form-item>
                  <el-form-item label="是否显示" prop="delivery">
                    <el-switch
                      v-model="editruleForm.value"
                      active-color="#13ce66"
                      inactive-color="#ff4949"
                    >
                    </el-switch>
                  </el-form-item>
                </el-form>
              </span>
              <span slot="footer" class="dialog-footer">
                <el-button @click="isdialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editAddList(row)"
                  >确 定</el-button
                >
              </span>
            </el-dialog>
            <el-link type="primary" @click="delList(row)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="prev, pager, next,total"
        :total="counts"
        @current-change="pageChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { list, remove, add, update } from '../../api/hmmm/subjects'
import { parseTimeByString } from '@/filters'
export default {
  data () {
    return {
      editIsFrontDisplay: '',
      editID: '',
      // 是否显示对话框
      dialogVisible: false,
      isdialogVisible: false,
      // 分页
      counts: 0,
      // input框数据
      input: '',
      page: 1,
      pagesize: 10,
      // 主体数据
      tableData: [],
      subjectName: '',
      ruleForm: {
        name: '',
        value: true,
        num: ''
      },
      editruleForm: {
        name: '',
        value: true,
        num: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 获取数据
    async getList () {
      try {
        const { page, pagesize } = this
        const params = {
          page,
          pagesize,
          subjectName: this.input
        }
        const { data: res } = await list(params)
        // 分页有多少数据
        this.counts = res.counts
        // 数据列表
        this.tableData = res.items
        // 当前页
        this.page = res.page
        // 获取条数
        this.pagesize = res.pagesize
      } catch (error) {
        this.$message.error(error)
      }
    },
    // 清空 input 框
    clearInput () {
      this.input = ''
    },
    // 搜索框
    lookupList () {
      try {
        this.getList()
        this.$message.success('数据已经渲染出来了哦!')
      } catch (error) {
        this.$message.error('数据为空')
      }
    },
    // 添加数据
    addList () {
      this.dialogVisible = true
      if (this.ruleForm.value === true) {
        this.ruleForm.num = 1
      } else {
        this.ruleForm.num = 0
      }
      const { name, num } = this.ruleForm

      const params = {
        name,
        num
      }
      try {
        add({ subjectName: name, isFrontDisplay: num })
        this.$message.success('添加成功')
        this.getList()
      } catch (error) {
        this.$message.error('添加失败')
      }

      this.dialogVisible = false
      this.ruleForm.name = ''
    },
    // 弹出框
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => { })
    },
    // 跳转到目录组件
    goCatalog (row) {
      this.$router.push(`directorys?id=${row.id}&name=${row.subjectName}`)
      // this.$router.push({ path: 'directorys', name: "subjects-directorys", params: { id: row.id, name: row.subjectName } })
    },
    // 跳转到标签组件
    golabel (row) {
      this.$router.push(`tags?id=${row.id}&name=${row.subjectName}`)
      // this.$router.push({ path: `tags`, name: "subjects-tags", params: { id: row.id, name: row.subjectName } })

    },
    // 修改按钮
    editList (row) {
      console.log(row.subjectName, row.id, this.editruleForm.value)
      this.editID = row.id
      this.isdialogVisible = true
      this.editruleForm.name = row.subjectName
      if (row.isFrontDisplay === 1) {
        // editIsFrontDisplay: '',
        this.editIsFrontDisplay = true
      } else {
        this.editIsFrontDisplay = false
      }
      this.editruleForm.value = this.editIsFrontDisplay

    },
    // 修改完添加的按钮
    async editAddList () {
      this.editruleForm.value = this.editIsFrontDisplay
      if (this.editruleForm.value === true) {
        this.editruleForm.num = 1
      } else {
        this.editruleForm.num = 0
      }
      const { num } = this.editruleForm
      const data = {
        id: this.editID,
        isFrontDisplay: num,
        subjectName: this.editruleForm.name
      }
      const res = await update(data)
      if (res.status === 200) {
        this.$message.success('数据修改成功')
      }
      // try {
      //   update(params)
      //   this.$message('修改成功 ')
      // }
      // catch (error) {
      //   this.$message(error)
      // }
      this.getList()
      this.isdialogVisible = false
    },
    // 删除按钮
    async delList (id) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await remove({ id: id.id })
          this.$message.success({
            message: '删除成功！', type: 'success'
          })
          this.getList()
        } catch (error) {
          console.warn(error)
          this.$message.error({
            message: '删除失败！', type: 'error'
          })
        }
      }).catch(() => {
        console.log('取消')
      })
    },
    // 切换分页
    pageChange (e) {
      this.page = e
      try {
        this.getList()
        this.$message.success('切换成功')
      } catch (error) {
        this.$message.error('切换失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.subjects-container {
  .box-one {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    .one-left {
      display: flex;
      width: 50%;
      .one-left-span {
        font-weight: 700;
        line-height: 32px;
        font-size: 15px;
        font-style: "微软雅黑";
        color: #646466;
        padding-left: 15px;
      }
      .one-left-input {
        width: 200px;
        margin: 0 10px;
      }
      .el-input__inner {
        height: 32px;
      }
    }
  }
  .box-two {
    height: 30px;
    line-height: 30px;
    font-size: 13px;
    color: #909399;
    background-color: #f4f4f5;
    margin-bottom: 15px;
    border-radius: 4px;
    i {
      margin-left: 20px;
      margin-right: 10px;
    }
  }
  .el-table {
    margin-bottom: 15px;
  }
  .el-link {
    margin-right: 10px;
  }
  .el-pagination {
    margin: 20px 0;
    float: right;
    /deep/ .el-pagination__jump {
      margin: 0;
    }
  }
  .el-dialog {
    .dialog-footer {
      padding-left: 255px;
    }
  }
}
</style>
